const fs = require('fs');
const path = require('path');

const gitHookPath = path.join(__dirname, '../../.git/hooks');

new Promise((resolve) => {
  fs.mkdir(gitHookPath, { recursive: true }, (err) => {
    if (err) {
      console.error('创建.git/hooks文件夹失败！');
      throw err;
    }
    resolve();
  });
}).then(() => {
  const preCommitReadStream = fs.createReadStream(path.join(__dirname, './pre-commit'));
  const preCommitPath = path.join(gitHookPath, 'pre-commit');
  const preCommitWriteStream = fs.createWriteStream(preCommitPath);
  preCommitReadStream.pipe(preCommitWriteStream).on('finish', () => {
    fs.chmodSync(preCommitPath, '755');
  });
});
